package com.small.springframework.beans.factory.support;

import com.small.springframework.beans.factory.config.BeanDefinition;

import java.lang.reflect.Constructor;

/**
 * @Author zhoujin
 * @Date 2022/3/29 20:23
 */
public class SimpleInstantiationStrategy implements InstantiationStrategy{

    @Override
    public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor constructor, Object[] args) {
        Class beanClass = beanDefinition.getBeanClass();
        Object object = null;
        try {
            if (constructor == null){
                return beanClass.getDeclaredConstructor().newInstance();
            }
            object = beanClass.getDeclaredConstructor(constructor.getParameterTypes()).newInstance(args);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }
}
